package com.example.demo.service;

import com.example.demo.bean.TimetableBean;
import com.example.demo.bean.UserTimetableZJBean;
import com.example.demo.repository.TimetableRepository;
import com.example.demo.repository.UserTimetableZJRepository;
import com.example.demo.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;
import java.util.Map;

@Transactional
@Service
public class TimetableService {

    @Autowired
    TimetableRepository timetableRepository;

    @Autowired
    UserTimetableZJRepository userTimetableZJRepository;

    public void addTimetable(TimetableBean timetableBean) {
        timetableRepository.save(timetableBean);
    }

    public void delTimetable(String objId) {
        timetableRepository.deleteByObjId(objId);
        userTimetableZJRepository.deleteByTimetableId(objId);
    }

    public List<Map<String, Object>> timetableList(int page, int pagesize, String userId,String class_name) {
        Sort.Order order = new Sort.Order(Sort.Direction.DESC, "create_time");
        Pageable pageable = PageRequest.of(page, pagesize, Sort.by(order));
        List<Map<String, Object>> list = null;
        if(StringUtils.isEmpty(userId)){
            list = timetableRepository.findAllTimeableList(class_name,pageable).getContent();
        }else{
            list = timetableRepository.findTimeableByUserList(pageable, userId).getContent();
        }
        return list;
    }

    public List<Map<String, Object>> findUserTimeableZJ(String timetableId){
        List<Map<String, Object>> list = userTimetableZJRepository.findUserTimeTableBean(timetableId);
        return list;
    }

    public void addUserTimeableZJ(UserTimetableZJBean bean) {
        userTimetableZJRepository.save(bean);
    }

    public void deleteTeacher(String timetableId) {
        userTimetableZJRepository.deleteByTimetableId(timetableId);
    }

    public List<Map<String, Object>> findTimeableZJ(String user_id){
        List<Map<String, Object>> list = userTimetableZJRepository.findTimeTableBean(user_id);
        return list;
    }
}
